<?php
include_once('config/config.php');

$LoginAdmin = auth();

$id = isset($_GET['id']) ? $_GET['id'] : 0;

$AdminSql = "SELECT * FROM `stu_admin` WHERE `id` = '$id'";

$Admin = find($AdminSql);

if($_POST)
{
    // 封装数组
    $data = [
        'nickname' => $_POST['nickname']
    ];

    // 处理密码 => 同时填了密码以及确认密码 = 更改密码
    if(!empty($_POST['password']) && !empty($_POST['repass']))
    {
        /* 
            1、验证密码与确认密码是否一致
            2、生成一个新的密码盐
            3、md5加密
            4、加密的密码和密码盐存入data
        */
        if($_POST['password'] != $_POST['repass'])
        {
            ShowMsg('密码与确认密码不一致');
        }

        // 生成新的密码盐
        $salt = build_ranstr();

        $password = md5($_POST['password'].$salt);

        $data['password'] = $password;

        $data['salt'] = $salt;
        
    }

    // 判断是否有上传头像
    if(isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0)
    {
        // 调用上传函数
        $avatar = upload_img('avatar');

        if($avatar['code'] == 0)
        {
            ShowMsg($avatar['msg']);
        }

        $data['avatar'] = $avatar['data'];
    }

    $result = update('admin',$data,"`id` = '$id'");

    if($result)
    {
        if(isset($data['avatar']) && $_FILES['avatar']['size'] > 0)
        {
            @is_file($Admin['avatar']) && @unlink($Admin['avatar']);
        }

        if(($LoginAdmin['id'] == $Admin['id']) && ($LoginAdmin['username'] == $Admin['username']))
        {
            $adminSql = "SELECT * FROM `stu_admin` WHERE `id` = '$id'";

            $admin = find($adminSql);

            setcookie('LoginAdmin',json_encode($admin));
        }

        ShowMsg('更新成功','adminlist.php');
    }else{
        if(isset($data['avatar']) && $_FILES['avatar']['size'] > 0)
        {
            @is_file($data['avatar']) && @unlink($data['avatar']);
        }

        ShowMsg('更新失败','adminlist.php');
    }

    
}



if(!$Admin)
{
    ShowMsg('该管理员不存在，请重新选择');
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <?php include_once('common/meta.php') ?>
</head>

<!--[if lt IE 7 ]> <body class="ie ie6"> <![endif]-->
<!--[if IE 7 ]> <body class="ie ie7 "> <![endif]-->
<!--[if IE 8 ]> <body class="ie ie8 "> <![endif]-->
<!--[if IE 9 ]> <body class="ie ie9 "> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!-->
<style>
  input[type="file"]{
    display: none;
  }

  label[for="avatar"]{
    width: 200px;
  }
</style>
<body>
  <!--<![endif]-->

  <!-- 引用头部 -->
  <?php include_once('common/header.php') ?>

  <!-- 引用菜单 -->
  <?php include_once('common/nav.php') ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">新增管理员</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li><a href="adminlist.php">AdminList</a> <span class="divider">/</span></li>
      <li class="active">AdminAdd</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='adminlist.php'"><i class="icon-list"></i> 管理员列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">

                <label>用户名</label>
                <input type="text" name="username" disabled class="input-xxlarge" placeholder="请输入用户名" value="<?php echo $Admin['username'] ?>">

                <label>昵称</label>
                <input type="text" name="nickname" required class="input-xxlarge" placeholder="请输入昵称" value="<?php echo $Admin['nickname'] ?>">
                
                <label>密码</label>
                <input type="password" name="password" class="input-xxlarge" placeholder="密码为空不修改密码">
                
                <label>确认密码</label>
                <input type="password" name="repass" class="input-xxlarge" placeholder="确认密码为空不修改密码">

                <label>头像</label>
                <label for="avatar">
                  <img src="<?php echo $Admin['avatar'] ? $Admin['avatar'] : 'assets/images/upload.png' ?>" id="upload" alt="">
                </label>
                <input type="file" name="avatar" id="avatar" class="input-xxlarge" >

                <label></label>
                <input class="btn btn-primary" type="submit" value="提交" />
              </form>
            </div>
          </div>
        </div>

        <?php include_once('common/footer.php'); ?>

      </div>
    </div>
  </div>
</body>

</html>
<?php include_once('common/script.php'); ?>
<script>
  $('#avatar').change(function(){

    // 获取当前的文件
    var file = $(this)[0].files[0] ? $(this)[0].files[0] : {}

    // 返回对象地址
    var url = GetObjectURL(file)

    // 把图像更新
    $('#upload').attr('src',url)
  })
</script>